package com.lisa.passjava.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.lisa.common.es.QuestionEsModel;
import com.lisa.passjava.search.config.EsConstant;
import com.lisa.passjava.search.service.IElasticSearchQuestionService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Service("questionService")
public class ElasticSearchQuestionServiceImpl implements IElasticSearchQuestionService {
    @Autowired
    RestHighLevelClient restHighLevelClient;
    @Override
    public boolean save(QuestionEsModel questionEsModel) throws IOException {
        //创建数据到ES中
        IndexRequest indexRequest = new IndexRequest(EsConstant.QUESTION_INDEX);
        indexRequest.id(questionEsModel.getId().toString());
        String jsonString = JSON.toJSONString(questionEsModel);
        //插入数据到ES中
        indexRequest.source(jsonString, XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(response);
        return true;
    }
}
